<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 收藏 model
 *
 */
class Appmodel extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 检测给定的应用是否有效
     *
     * @param unknown_type $id
     */
    function check_app($id='', $uid='', $belong_user=1)
    {
    	$where = array('id' => $id);
    	if($uid && $belong_user){
    		$where['uid'] = $uid;
    	}elseif($uid && !$belong_user){
    		$where['uid <>'] = $uid;
    	}
    	
    	$this->db->select('count(*) as count');
		$this->db->where($where, FALSE);
		$query = $this->db->get('app');
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 检测用户给定的应用是否存在
     *
     * @param unknown_type $uid
     * @param unknown_type $name
     */
    function check_app_by_name($uid='', $name='', $version='')
    {
    	$where = array('uid' => $uid, 'name' => $name);
    	//if($version) 
    	$where['version'] = $version;
    	$this->db->select('count(*) as count');
		$query = $this->db->get_where('app', $where);
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 获取某一用户的应用列表
     *
     * @param unknown_type $uid
     */
    function get_apps($uid='')
    {
    	$result = array();
    	$this->dblib->set_query_limit();
    	$this->db->order_by('sharedtime', 'desc');
    	$query = $this->db->get_where('app', array('uid' => $uid));
    	if ($query->num_rows() > 0) {
			//如果有结果集
			$this->load->library('uploadimglib');
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['photo'] = $this->uploadimglib->re_image_path($v['photo']);
			}
		}
		return $result;
    }
    
    /**
     * 获取具体一条应用信息
     *
     * @param unknown_type $id
     */
    function get_app($id='')
    {
    	$this->load->library('uploadimglib');
    	$this->db->limit(1);
    	$query = $this->db->get_where('app', array('id' => $id));
    	$result = $query->result_array();
    	$result && $result[0]['photo'] = $this->uploadimglib->re_image_path($result[0]['photo']);
    	return $result;
    }
    
    /**
     * 根据条件获取唯一的应用信息
     *
     * @param unknown_type $uid
     * @param unknown_type $name
     * @param unknown_type $version
     */
    function get_app_by_name($uid='', $name='', $version='')
    {
    	$this->db->limit(1);
    	$query = $this->db->get_where('app', array('uid' => $uid, 'name' => $name, 'version' => $version));
    	return $query->result_array();
    }
    
    /**
     * 根据是否已经分享筛选
     *
     * @param unknown_type $uid
     * @param unknown_type $shared
     */
    function get_apps_shared1($uid='', $shared=1)
    {
    	$result = array();
    	$this->dblib->set_query_limit();
    	$this->db->order_by('sharedtime', 'desc');
    	$query = $this->db->get_where('app', array('uid' => $uid, 'share' => $shared));
    	if ($query->num_rows() > 0) {
			//如果有结果集
			$this->load->library('uploadimglib');
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['photo'] = $this->uploadimglib->re_image_path($v['photo']);
			}
		}
		return $result;
    }
    
    /**
     * 根据是否已经分享筛选(包括用户的photo)
     *
     * @param unknown_type $uid
     * @param unknown_type $shared
     */
    function get_apps_shared($shared=1)
    {
    	$result = array();
    	$this->dblib->set_query_limit();

    	$this->db->select('a.id as app_id, a.name as app_name, a.version, a.photo as app_photo, (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(a.sharedtime)) as diff_time, u.id as user_id, u.name as user_name, u.photo as user_photo, u.reg_type, c.content')
    			 ->from('app as a')
    			 ->join('user as u', 'u.id=a.uid')
    			 ->join('comments as c', 'c.comment_id=a.id AND c.uid=a.uid AND share_or_comment=' .IS_SHARE. ' AND c.comment_type=' .APP_COMMENT, 'left')
    			 ->where(array('a.share' => $shared))
    			 ->group_by('a.id')
    			 ->order_by('sharedtime', 'desc');
    	$query = $this->db->get();
    	if ($query->num_rows() > 0) {
			//如果有结果集
			$this->load->library('uploadimglib');
			$this->load->library('timelib');//处理时间
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['diff_time'] = $this->timelib->diff_time($v['diff_time']);
				$result[$k]['app_photo'] = $this->uploadimglib->re_image_path($v['app_photo']);
				$result[$k]['user_photo'] = $this->uploadimglib->re_user_photo($v['user_photo'], $v['reg_type']);
				unset($result[$k]['reg_type']);//不需要此字段
			}
		}
		return $result;
    }
    
    /**
     * 更新以分享
     *
     * @param unknown_type $uid
     * @param unknown_type $appid
     */
    function set_app_shared($appid='', $shared=1)
    {
    	$this->db->set('sharedtime', '(SELECT NOW())', FALSE);//设置分享时间
    	return $this->db->update('app', array('share' => $shared), array('id' => $appid));
    }
    
    /**
     * 检测给定的应用是否以分享
     *
     * @param unknown_type $uid
     * @param unknown_type $appid
     * @param unknown_type $shared
     * @return unknown
     */
    function check_app_shared($appid='', $shared=1)
    {
    	$this->db->select('count(*) as count');
		$this->db->where(array('share' => $shared, 'id' => $appid));
		$query = $this->db->get('app');
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 取得已安装软件数量
     *
     * @param unknown_type $uid
     * @param unknown_type $install
     * @return unknown
     */
    function get_apps_install_count($uid='', $install=1)
    {
    	$this->db->select('count(*) as count')
    			 ->from('app')
    			 ->where(array('uid' => $uid, 'install' => $install));
    	$query = $this->db->get();
    	$row = $query->row_array();
    	return $row['count'];
    }
    
    /**
     * 添加应用
     *
     * @param unknown_type $data
     */
    function insert_app($data=array())
    {
    	if(empty($data) || !isset($data['uid']) || empty($data['uid']) ||
    				!isset($data['name']) || empty($data['name'])){
    					return false;
    	}
    	$this->db->set('id', '(SELECT UUID())', FALSE);
    	$this->db->set('sharedtime', '(SELECT NOW())', FALSE);
    	return $this->db->insert('app', $data);
    }
    
    /**
     * 更新应用图片
     *
     * @param unknown_type $photo
     */
    function update_photo($id='', $photo='')
    {
    	if (empty($id) || empty($photo)) return FALSE;
		//$this->db->set('photo', $photo);
		return $this->db->update('app', array('photo' => $photo), array('id' => $id));
    }
    
}

/* End of file appmodel.php */
/* Location: ./application/models/appmodel.php */